package com.ld.security.common.core;

import org.omg.SendingContext.RunTime;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.sql.Struct;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;


@Configuration
@ComponentScan("com.ld.security.common.core")
public class ThreadConfig {

    /**
     * 线程空间时间存活时间秒数
     */
    long maxTime = 60;


    /**
     * 一般线程池
     * @return
     */
    @Bean
    @Primary
    public ThreadPoolExecutor threadPoolExecutor(){
        int coolSize = Runtime.getRuntime().availableProcessors() + 1;
        int maxSize = coolSize * 2;
        ArrayBlockingQueue queue = new ArrayBlockingQueue(coolSize*50);
        ThreadPoolExecutor threadPoolExecutor =new ThreadPoolExecutor(coolSize,maxSize,maxTime,TimeUnit.SECONDS,queue,new ThreadPoolExecutor.CallerRunsPolicy());

        return threadPoolExecutor;
    }

    /**
     * 定时任务
     * @return
     */
    @Bean
    public ScheduledThreadPoolExecutor scheduledThreadPoolExecutor(){
        int coolSize = Runtime.getRuntime().availableProcessors() + 1;
        int maxSize = coolSize * 2;

        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =new ScheduledThreadPoolExecutor(coolSize);
        scheduledThreadPoolExecutor.setKeepAliveTime(maxTime,TimeUnit.SECONDS);
        scheduledThreadPoolExecutor.setMaximumPoolSize(maxSize);

        return scheduledThreadPoolExecutor;
    }

}
